package cn.com.cetccst.basiccollection.peroid;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

import cn.com.cetccst.basiccollection.InfoType;
import cn.com.cetccst.basiccollection.Collector;
import cn.com.cetccst.basiccollection.BasicInfoEngine;
import cn.com.cetccst.datacenter.collection.CollectionRepository;
import cn.com.cetccst.datacenter.collection.entity.CollectionDataEntity;

//这里使用Worker是否合理？因为Worker最低间隔15min
public class CollectWorker extends Worker {
    public CollectWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @NonNull
    @Override
    public Result doWork() {
        String type = getInputData().getString("type");
        Collector collector = BasicInfoEngine.getInstance().getCollector(InfoType.valueOf(type));
        Log.i(PeroidCollectHelper.TAG, "collect worker type: " + type);
        if (collector == null) {

        } else {
            String info = collector.collect();
            Log.i(PeroidCollectHelper.TAG, "collect worker info: " + info);
            CollectionDataEntity data = new CollectionDataEntity();
            data.setCollectData(info);
            data.setCollectType(collector.getName());
            data.setCollectTime(collector.timestamp());
            CollectionRepository.getInstance().getCollectionDataDao().insert(data);
        }
        return Result.success();
    }
}
